package com.raising.modules.sys.controller;

import com.raising.StaticParam;
import com.raising.framework.controller.BaseController;
import com.raising.framework.entity.ResultCode;
import com.raising.framework.entity.ResultVo;
import com.raising.framework.interceptor.Page;
import com.raising.modules.sys.entity.Resource;
import com.raising.modules.sys.entity.enums.ResourceEnum;
import com.raising.modules.sys.service.ResourceService;
import com.raising.modules.sys.service.UserService;
import com.raising.modules.sys.utils.DictUtils;
import com.raising.utils.JStringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 资源控制器
 * 
 * @author gaoy
 */
@Controller
@RequestMapping("${adminPath}/resource")
public class ResourceController extends BaseController {

	@Autowired
    ResourceService resourceService;
    @Autowired
    UserService userService;

	@ModelAttribute("types")
	public ResourceEnum[] resourceTypes() {
		return ResourceEnum.values();
	}

    /**
     * 进入菜单管理列表页
     * @author gaoyuan
     * @param resource
     * @param model
     * @return
     */
	@RequiresPermissions("sys:resource:view")
	@RequestMapping()
	public String list(Resource resource,Model model) {
        Resource resource1 = new Resource();
        //树型数据，只看菜单
        resource1.setType(ResourceEnum.menu);
        if( JStringUtils.isNoneBlank(resource.getParentId()) ){
            model.addAttribute("parentId",resource.getParentId());
        }
        olog("进入菜单管理页面");
        return "sys/resource/list";
	}

	/**
	 * 查询菜单分页数据
	 * @author GaoYuan
	 * @param resource
     * @param page
	 * @datetime 2018/2/23 上午10:16
	 */
    @RequiresPermissions("sys:resource:view")
    @RequestMapping(value = "/getPage.json", method = RequestMethod.POST)
    public @ResponseBody ResultVo getPage(Resource resource, Page<Resource> page) throws Exception{
	    //初始化加载第一个
        if(JStringUtils.isBlank(resource.getId())){
            //默认加载
            resource.setId("1");
        }
        page.setEntity(resource);
        return ResultVo.success().data(resourceService.getPage(page));
    }

    /**
     * 获取菜单
     * @author GaoYuan
     * @date 2018/11/21 上午11:55
     */
    @RequiresPermissions("sys:resource:view")
    @RequestMapping(value = "/getMenus.json", method = RequestMethod.GET)
    public @ResponseBody ResultVo getMenus() throws Exception{
        Resource resource = new Resource();
        //树型数据，只看菜单
        resource.setType(ResourceEnum.menu);
        return ResultVo.success().data(resourceService.getList(resource));
    }

    /**
     * 增加四项权限
     * @author GaoYuan
     * @datetime 2018/2/23 上午10:18
     */
    @RequiresPermissions("sys:resource:create")
    @RequestMapping(value = "/createFour.json", method = RequestMethod.POST)
    public @ResponseBody ResultVo createFour(String resourceId,String name) throws Exception{
	    Resource resource_query = resourceService.get(resourceId);
        Resource resource = new Resource();
        resource.setName("查看");
        resource.setType(ResourceEnum.form);
        resource.setParentId(resourceId);
        resource.setParentIds(resource_query.getParentIds() + resourceId + "/");
        resource.setPermission(name+"view");
        resource.setAvailable(true);
        resource.setTarget("_self");
        resource.setSort(1);
        resource.setStatus(StaticParam.VALID_STRING);
        resource.preInsert(null);
        resource.setUrl("");
        resource.setIcon("");
        resource.setModuleCd(resource_query.getModuleCd());
        resourceService.insert(resource);

        Resource resource2 = new Resource();
        resource2.setName("添加");
        resource2.setType(ResourceEnum.form);
        resource2.setParentId(resourceId);
        resource2.setParentIds(resource_query.getParentIds() + resourceId + "/");
        resource2.setPermission(name+"create");
        resource2.setAvailable(true);
        resource2.setTarget("_self");
        resource2.setSort(1);
        resource2.setStatus(StaticParam.VALID_STRING);
        resource2.preInsert(null);
        resource2.setUrl("");
        resource2.setIcon("");
        resource2.setModuleCd(resource_query.getModuleCd());
        resourceService.insert(resource2);

        Resource resource3 = new Resource();
        resource3.setName("编辑");
        resource3.setType(ResourceEnum.form);
        resource3.setParentId(resourceId);
        resource3.setParentIds(resource_query.getParentIds() + resourceId + "/");
        resource3.setPermission(name+"update");
        resource3.setAvailable(true);
        resource3.setTarget("_self");
        resource3.setSort(1);
        resource3.setStatus(StaticParam.VALID_STRING);
        resource3.preInsert(null);
        resource3.setUrl("");
        resource3.setIcon("");
        resource3.setModuleCd(resource_query.getModuleCd());
        resourceService.insert(resource3);

        Resource resource4 = new Resource();
        resource4.setName("删除");
        resource4.setType(ResourceEnum.form);
        resource4.setParentId(resourceId);
        resource4.setParentIds(resource_query.getParentIds() + resourceId + "/");
        resource4.setPermission(name+"delete");
        resource4.setAvailable(true);
        resource4.setTarget("_self");
        resource4.setSort(1);
        resource4.setStatus(StaticParam.VALID_STRING);
        resource4.preInsert(null);
        resource4.setUrl("");
        resource4.setIcon("");
        resource4.setModuleCd(resource_query.getModuleCd());
        resourceService.insert(resource4);
        olog("添加菜单["+resource_query.getName()+"]四项权限","{id:"+resourceId+"}");
        return ResultVo.success().desc("成功添加");
    }


    /**
     * 跳转到新增页
     * @author gaoyuan
     * @param model
     * @return
     */
    @RequiresPermissions("sys:resource:edit")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create( Model model) throws Exception{
        //树型数据，只看菜单
        Resource queryParam = new Resource();
        queryParam.setType(ResourceEnum.menu);
        queryParam.setStatus(StaticParam.VALID_STRING);
        List<Resource> resourceList = resourceService.getList(queryParam);
        model.addAttribute("resourceList", resourceList);
        // 是否可用
        model.addAttribute("yn", DictUtils.getDictList("DIM_YN"));
        return "sys/resource/add";
    }

    /**
     * 跳转到修改页
     * @author gaoyuan
     * @param model
     * @return
     */
    @RequiresPermissions("sys:resource:edit")
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(String id, Model model) throws Exception{
        Resource resource = resourceService.get(id);
        model.addAttribute("resource", resource);
        //树型数据，只看菜单
        Resource queryParam = new Resource();
        queryParam.setType(ResourceEnum.menu);
        queryParam.setStatus(StaticParam.VALID_STRING);
        List<Resource> resourceList = resourceService.getList(queryParam);
        model.addAttribute("resourceList", resourceList);
        // 是否可用
        model.addAttribute("yn",DictUtils.getDictList("DIM_YN"));
        return "sys/resource/edit";
    }

    /**
     * 修改
     * @param resource
     * @return
     */
    @RequiresPermissions("sys:resource:edit")
    @RequestMapping(value = "/update.json", method = RequestMethod.POST)
    public @ResponseBody ResultVo updatePost(Resource resource) throws Exception{
        ResultVo resultVo = resourceService.save(resource);
        olog("修改菜单["+resource.getName()+"]信息","{id:"+resource.getId()+"}");
        return resultVo;
    }

    /**
     * 删除
     * @author gaoyuan
     * @return
     */
    @RequiresPermissions("sys:resource:edit")
    @RequestMapping(value = "/delete.json", method = RequestMethod.POST)
    public @ResponseBody ResultVo delete(String id) throws Exception{
        String rootNode = "1";
        if(!rootNode.equals(id)){
            //删除前判断是否存在下级节点
            int count = resourceService.findNext(id);
            if(count>0){
                return ResultVo.failure(ResultCode.NO_PERMISSIONS).desc("删除失败，存在子节点，请先删除子节点");
            }else{
                ResultVo resultVo = resourceService.delete(id);
                olog("删除菜单","{id:"+id+"}");
                return resultVo;
            }
        } else {
            return ResultVo.failure(ResultCode.NO_PERMISSIONS).desc("超级管理员才能删除");
        }
    }

}
